package com.yiqi.lottery.feature.mine.view;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.AppUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.CacheManager;
import com.yiqi.lottery.feature.login.contract.LoginContract;
import com.yiqi.lottery.feature.login.dependencies.login.DaggerLoginComponent;
import com.yiqi.lottery.feature.login.dependencies.login.LoginPresenterModule;

import javax.inject.Inject;


public class SeetingActivity extends BaseActivity implements LoginContract.View {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @Inject
    LoginContract.Presenter mPresenter;
    @BindView(R.id.btn_user_setting_quit)
    Button mBtnUserSettingQuit;
    @BindView(R.id.tv_setting_show_cache)
    TextView mTvSettingShowCache;
    @BindView(R.id.tv_user_seeting_version)
    TextView mTvUserSeetingVersion;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_seeting;
    }


    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().loginPresenterModule(new LoginPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "设置", true, true);
        if (AM.user().isLogin()) {
            mBtnUserSettingQuit.setVisibility(View.VISIBLE);
        } else {
            mBtnUserSettingQuit.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        String text =  "V" + AppUtil.getVersionName(this);
        mTvUserSeetingVersion.setText(text);

        try {
            mTvSettingShowCache.setText(CacheManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.btn_user_setting_version, R.id.btn_user_setting_about, R.id.btn_user_setting_clear, R.id.btn_user_setting_quit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user_setting_version:
                break;
            case R.id.btn_user_setting_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.btn_user_setting_clear:
                break;
            case R.id.btn_user_setting_quit:
                tipClick();
                break;
        }
    }

    @Override
    public void showLoginSuccess() {

    }

    @Override
    public void showLogOutSuccess() {
        T.showShort("退出成功!");
        AM.user().loginOut();
        finish();
    }

    @Override
    public void showLoginError(String error) {
        T.showShort(error);
    }

    @Override
    public void setUserAccount(String username, String password, boolean isRemember) {

    }

    public void tipClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("是否退出当前账号？");

        builder.setCancelable(true);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.userLogout();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @OnClick(R.id.tv_setting_show_cache)
    public void onViewClicked() {
        CacheManager.clearAllCache(this);
        PictureFileUtils.deleteCacheDirFile(this);
        try {
            T.showShort("清除成功!");
            String mDatas = CacheManager.getTotalCacheSize(this);
            if (!TextUtils.isEmpty(mDatas)) {
                mTvSettingShowCache.setText(mDatas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
